<template>
    <div class="requestedMyPhoto">
        <div class="moduleHead">
            <span class="center">
            <span class="back" @click="$router.go(-1)"></span>
            <span>{{language.requestedMyPhoto_title}}</span>
            </span>
        </div>
        <div class="body">
            <promptTip :props="promptData"></promptTip>
            <ul class="accessList">
                <li v-for="item in items" v-on:click="gotoUserProfile(item.usr_id)">
                    <div class="avatar">
                        <LazyImg :src="item.icon"></LazyImg>
                    </div>
                    <i v-if="item.checked == 0" class="unread"></i>
                    <div class="info">
                        <div class="name">{{ item.username }} </div>
                        <div v-if="!item.hide_profile" class="text location">
                            {{ item.baseInfo }}
                        </div>
                        <div class="text" v-else>{{language.unify_hideProfile}}</div>
                        <div class="text">
                            {{language.requestedMyPhoto_requested}} <span class="date">{{item.sent_date}}</span>
                        </div>
                    </div>
                </li>
            </ul>
            <loading :props="{show:loading}"></loading>
        </div>
    </div>
</template>
<script>
import * as API from '../api'
import LazyImg from './LazyImg'
import promptTip from './PromptTip'
import loading from './Loading'
import globalMixin from '../common/globalMixin'

let scrollFunc,
    userList = [], //局部大对象数组变量
    firstLoad = true,
    condition = {};

export default {

    name: 'requestedMyPhoto',

    components: {
        LazyImg,
        promptTip,
        loading
    },

    mixins: [globalMixin],

    data: function() {
        return {
            stateNums: {
                requestPhoto: 0,
            },
            loading: 0,
            userListLength: 0,
            promptData: {}
        }
    },

    created() {
        condition = {
            from: 0,
            offset: 10
        };
        firstLoad = true;
        userList = [];
        this.initData();
    },

    activated: function() {
        this.promptData = {};
        this.stateNums = this.getNums();
    },

    deactivated(){
        this.setNums();
    },

    computed: {

        items: function() {
            let data = [],
                tthis = this;
            _.forEach(userList.slice(0, tthis.userListLength), function(value, key) {
                data.push(tthis.formatUserInfo(value));
            });
            return data;
        },
    },

    methods: {

        getNums() {
            let stateObj = this.$store.state.base.siteNumState,
                obj = {
                    'requestPhoto': (typeof(stateObj) != 'undefined' && typeof(stateObj.request_photo_count) != 'undefined') ? this.judgeNum(stateObj.request_photo_count.new_count || 0) : 0,
                };
            // obj.requestPhoto = 10;
            return obj;
        },

        setNums(){
            let stateObj = Unit.getObjSessionStorage("siteNumState") || {};
            if (typeof(stateObj.request_photo_count) != 'undefined') {
                stateObj.request_photo_count.new_count = this.stateNums.requestPhoto;
            }
            this.$store.dispatch('updateSiteNumStateData', stateObj);
        },

        initData: function() {
            this.getReqList(condition);
        },

        getReqList: function(data) {
            let tthis = this;
            if (this.loading == 0) {
                this.loading = 1;
                API.who_request_photo({
                    data: data,
                    success: (response) => {
                        if (response.res.length == 0 && condition.from == 0) {
                            tthis.promptData = {
                                type: 'default',
                                content: tthis.language.requestedMyPhoto_nodataTip,
                                btn: {
                                    label: tthis.language.requestedMyPhoto_nodataBtn,
                                    callback: function() {
                                        tthis.$router.push('/browse');
                                    }
                                }
                            };
                            tthis.loading = 0;
                        } else if (response.res.length == 0) {
                            tthis.loading = 3;
                        } else {
                            firstLoad = false;
                            tthis.loading = 0;
                            userList = userList.concat(response.res);
                            tthis.userListLength = userList.length;
                            condition.from = condition.from + condition.offset;
                            tthis.stateNums.requestPhoto = 0;
                        }
                    }
                });
            }
        },

        onWinBottomScroll: function() {
            if (this.$route.name == 'requestedMyPhoto') {
                this.getReqList(condition);
            }
        },

        formatUserInfo(userInfo) {
            let data = {};
            data.username = userInfo.username;
            data.usr_id = userInfo.usr_id;
            data.hide_profile = parseInt(userInfo.hide_profile) == 1 ? true : false;
            data.icon = this.getAvatar(userInfo.gender, userInfo.picture.icon);
            data.baseInfo = this.formatUserLocation({
                age: userInfo.age,
                city: userInfo.city,
                state: userInfo.state,
                country: userInfo.country,
            });
            data.gender = userInfo.gender;
            data.sent_date = userInfo.sent_date;
            data.checked = userInfo.checked;
            return data;
        },

    }
}

</script>
<style scoped>
.requestedMyPhoto {
    padding-top: 44px;
}

.requestedMyPhoto .body {
    padding-bottom: 30px;
    background: #fff;
}

.requestedMyPhoto .accessList li {
    position: relative;
    width: 100%;
    min-height: 100px;
    overflow: hidden;
    border-bottom: 1px dotted #d9d9d9;
}
.requestedMyPhoto .accessList .unread {
    width: 10px;
    height: 10px;
    background: #DF4141;
    display: block;
    border-radius: 10px;
    top: 10px;
    left: 82px;
    position: absolute;
}
.requestedMyPhoto .accessList .info {
    margin: 0 15px 0 105px;
    overflow: hidden;
}

.requestedMyPhoto .accessList .date {
    color: rgb(102, 102, 102);
}

.requestedMyPhoto .accessList .name {
    margin: 10px 0 3px 0;
    padding: 0 40px 0 0;
    position: relative;
    overflow: hidden;
    height: 20px;
    line-height: 20px;
    font-size: 15px;
}

.requestedMyPhoto .accessList .text {
    font-size: 12px;
    line-height: 18px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    -o-text-overflow: ellipsis;
    color: #a3a4a6;
}

.requestedMyPhoto .ico_gold {
    background: url(../assets/ico_gold.png) center no-repeat;
    background-size: auto 17px;
    padding: 2px 0 2px 17px;
    margin-right: 5px;
}

.requestedMyPhoto .accessList li:last-child {
    order-bottom: 0;
}

.requestedMyPhoto .accessList .avatar {
    position: absolute;
    width: 70px;
    height: 70px;
    border-radius: 35px;
    overflow: hidden;
    left: 15px;
    top: 15px;
    background: url(../assets/default_avatar.png) no-repeat;
    background-size: 70px 70px;
    -webkit-mask-image: -webkit-radial-gradient(circle, white, black);
}

.requestedMyPhoto .nodata {
    position: absolute;
    top: 60px;
    width: 300px;
    margin: auto;
    left: 0;
    right: 0;
    font-size: 15px;
    color: #252525;
    border-radius: 3px;
    text-indent: 20px;
    padding: 10px;
}

.requestedMyPhoto .nodataText {
    border: 1px dashed #ccc;
    margin: auto;
    font-size: 15px;
    color: #252525;
    border-radius: 3px;
    text-indent: 20px;
    padding: 10px;
}

.requestedMyPhoto button {
    margin: 20px 0;
}

</style>
